package com.it.gw.manager

import android.util.Log
import com.blankj.utilcode.util.AppUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.Api
import com.it.data.network.rsp.DictionariesVo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object DictionariesManager {
    const val TAG = "DictionariesManager"



    const val ENG_GRAD_1 = "ENG_GRAD_1"//工程等别
    const val ENG_SCAL_1 = "ENG_SCAL_1"//工程规模
    const val RES_TYPE = "RES_TYPE"//水库类型
    const val RV_TYPE = "RV_TYPE"//河流类型
    const val RV_GRAD = "RV_GRAD"//河流级别

    const val BANK = "BANK"//岸别

    const val CR_OVER_TYPE = "CR_OVER_TYPE"//跨界类型

    const val SAL_FRE_WAT = "SAL_FRE_WAT"//咸淡水属性
    const val PUST_TYPE = "PUST_TYPE"//泵站类型
    const val MAIN_BUILD_GRAD = "MAIN_BUILD_GRAD"// 首部+主要建筑物级别
    const val ENG_STAT = "ENG_STAT"//主要建筑物级别
    const val ADM_DEP = "ADM_DEP"//归口管理部门
    const val DPC_ENG_GRAD_1 = "DPC_ENG_GRAD_1"
    const val MAJ_WASO_TYPE = "MAJ_WASO_TYPE"//主要水源工程类型
    const val SUP_WASO_TYPE = "SUP_WASO_TYPE"//补充水源工程类型

    const val wasoType = "wasoType"//水源类型
    const val wellType = "wellType"//水井类型
    const val wellUseType = "wellUseType"//水井用途



    val dictionariesMap =
        hashMapOf(
            "BANK" to mutableListOf<DictionariesVo>(),
            "ENG_GRAD_1" to mutableListOf<DictionariesVo>(),
            "ENG_SCAL_1" to mutableListOf<DictionariesVo>(),
            "WAGA_TYPE" to mutableListOf<DictionariesVo>(),
            "CR_OVER_TYPE" to mutableListOf<DictionariesVo>(),
            "SAL_FRE_WAT" to mutableListOf<DictionariesVo>(),
            "RES_TYPE" to mutableListOf<DictionariesVo>(),
            "MAIN_BUILD_GRAD" to mutableListOf<DictionariesVo>(),
            "PUST_TYPE" to mutableListOf<DictionariesVo>(),
            "ADM_DEP" to mutableListOf<DictionariesVo>(),
            "ENG_STAT" to mutableListOf<DictionariesVo>(),
            "MAJ_WASO_TYPE" to mutableListOf<DictionariesVo>(),
            "DPC_ENG_GRAD_1" to mutableListOf<DictionariesVo>(),
            "SUP_WASO_TYPE" to mutableListOf<DictionariesVo>(),
            "RV_GRAD" to mutableListOf<DictionariesVo>(),
            "RV_TYPE" to mutableListOf<DictionariesVo>(),
            "wasoType" to mutableListOf<DictionariesVo>(),
            "wellType" to mutableListOf<DictionariesVo>(),
            "wellUseType" to mutableListOf<DictionariesVo>(),
        )


    val shuikuMap =
        hashMapOf(
            "ENG_GRAD_1" to mutableListOf<DictionariesVo>(),
            "ENG_SCAL_1" to mutableListOf<DictionariesVo>(),
            "RES_TYPE" to mutableListOf<DictionariesVo>(),
            "RES_TYPE" to mutableListOf<DictionariesVo>(),
        )
    fun getShuiKuDictionaries() {
        shuikuMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val heliuMap =
        hashMapOf(
            CR_OVER_TYPE to mutableListOf<DictionariesVo>(),
            RV_TYPE to mutableListOf<DictionariesVo>(),
            RV_GRAD to mutableListOf<DictionariesVo>(),
            BANK to mutableListOf<DictionariesVo>(),

        )
    fun getHeliuDictionaries() {
        heliuMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val huboMap =
        hashMapOf(
            CR_OVER_TYPE to mutableListOf<DictionariesVo>(),
            SAL_FRE_WAT to mutableListOf<DictionariesVo>(),
            )

    fun getHuboDictionaries() {
        huboMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }
            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val shuizhaMap =
        hashMapOf(
            ENG_GRAD_1 to mutableListOf<DictionariesVo>(),
            ENG_SCAL_1 to mutableListOf<DictionariesVo>(),
        )

    fun getShuiZhaDictionaries() {
        shuizhaMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val bengzhanMap =
        hashMapOf(
            PUST_TYPE to mutableListOf<DictionariesVo>(),
            ENG_GRAD_1 to mutableListOf<DictionariesVo>(),
            ENG_SCAL_1 to mutableListOf<DictionariesVo>(),
            ENG_STAT to mutableListOf<DictionariesVo>(),
            ADM_DEP to mutableListOf<DictionariesVo>(),
            MAIN_BUILD_GRAD to mutableListOf<DictionariesVo>(),


        )

    fun getBengZhanDictionaries() {
        bengzhanMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }
    // 首部
    val shoubuMap =
        hashMapOf(
            DPC_ENG_GRAD_1 to mutableListOf<DictionariesVo>(),
            MAIN_BUILD_GRAD to mutableListOf<DictionariesVo>(),

            )
    fun getShouBuDictionaries() {
        shoubuMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    //斗渠
    val douquMap =
        hashMapOf(
            ENG_GRAD_1 to mutableListOf<DictionariesVo>(),
            DPC_ENG_GRAD_1 to mutableListOf<DictionariesVo>(),
            MAIN_BUILD_GRAD to mutableListOf<DictionariesVo>(),

            )

    val jidianjingMap =
        hashMapOf(
            wasoType to mutableListOf<DictionariesVo>(),
            wellType to mutableListOf<DictionariesVo>(),
            wellUseType to mutableListOf<DictionariesVo>(),

            )

    fun getJDJDictionaries() {
        jidianjingMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val guanquMap =
        hashMapOf(
            CR_OVER_TYPE to mutableListOf<DictionariesVo>(),
            MAJ_WASO_TYPE to mutableListOf<DictionariesVo>(),
            SUP_WASO_TYPE to mutableListOf<DictionariesVo>(),
            ENG_SCAL_1 to mutableListOf<DictionariesVo>(),

            )
    //灌区
    fun getGuanQuDictionaries() {
        guanquMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    if(dictionariesMap[k].isNullOrEmpty()){
                        val res = Api.apiClient().getDictionaries(type = k)
                        if (res.success == true && res.data.isNotEmpty()) {
                            if(dictionariesMap[k].isNullOrEmpty()){
                                dictionariesMap[k] = res.data!!.toMutableList()
                            }
                        }
                        Log.d(
                            TAG,
                            "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                        )
                    }
                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }



    fun getDictionaries() {
        dictionariesMap.forEach { k, v ->
            try {
                GlobalScope.launch(Dispatchers.IO) {
                    // Log.d(TAG, "wellUseType: ${k},${v}")
                    val res = Api.apiClient().getDictionaries(type = k)
                    if (res.success == true && res.data.isNotEmpty()) {
                        dictionariesMap[k] = res.data!!.toMutableList()
                    }
                    Log.d(
                        TAG,
                        "getDictionaries: res ${k} and ${res.data?.size} and ${res.data?.get(0)}"
                    )

                }

            } catch (e: Exception) {
                Log.d(TAG, "getDictionaries: e ${e.message}")
            }
        }

    }

    val yesOrNoDicts = mutableListOf(
        DictionariesVo(ITEMNAME = "是", ITEMCODE = "1"),
        DictionariesVo(ITEMNAME = "否", ITEMCODE = "0")
    )
    val sblxList =
        mutableListOf<DictionariesVo>(
            DictionariesVo(ITEMCODE = "供水", ITEMNAME = "供水"),
            DictionariesVo(ITEMCODE = "供排结合", ITEMNAME = "供排结合"),
            DictionariesVo(ITEMCODE = "排水", ITEMNAME = "排水")
        )

    fun findNameByCode(list: List<DictionariesVo>?, code: String?): String? {
        val data = list?.find { it.ITEMCODE == code }
        if (data.isNotEmpty()) {
            return data!!.ITEMNAME;
        } else {
            return null
        }

    }

    fun findCodeByName(list: List<DictionariesVo>?, name: String?): String? {
        val data = list?.find { it.ITEMNAME == name }
        if (data.isNotEmpty()) {
            return data!!.ITEMCODE;
        } else {
            return null
        }

    }
    val orderTypeList =
        mutableListOf(
            Pair<String, String>("0", "未知"),
            Pair<String, String>("1", "运维巡检"),
            Pair<String, String>("2", "离线站点"),
            Pair<String, String>("3", "人工上报问题")
        )

}